<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable class="!w-120px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMS_TASK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          @click="handleScan()"
          v-hasPermi="['cms:task-sensitive-result:create']"
        >
          扫描
        </el-button>
        <el-button
          type="primary"
          @click="handleDeleteTasks()"
          v-hasPermi="['cms:task-sensitive-result:delete']"
        >
          删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      ref="tableRef"
      row-key="id"
      @selection-change="handleDataSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="beginTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="完成数量" align="center" prop="currentNum" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_TASK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('detail', scope.row.id)">
            详情
          </el-button>
          <el-button link type="primary" @click="handleReusltPage(scope.row.id)">
            查看结果
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:model:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：详情 -->
  <TaskForm ref="formRef" @success="getList" />
  <!--相关内容-->
  <Dialog v-model="resultDialogVisible" title="查看结果" width="50%">
    <ContentWrap>
      <el-table
        v-loading="resultLoading"
        :table-layout="tableLayout"
        :data="resultDatas"
        ref="resultTableRef"
        row-key="id"
      >
        <el-table-column label="内容id" align="center" prop="contentId" />
        <el-table-column label="内容标题" align="center" prop="contentTitle" />
        <el-table-column label="敏感词" align="center" prop="sensitiveWord" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="relateTotal"
        v-model:page="taskResultQueryParams.pageNo"
        v-model:limit="taskResultQueryParams.pageSize"
        @pagination="getTaskResultList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script setup lang="ts" name="Task">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { TaskVO } from '@/api/cms/task/index'
import * as TaskApi from '@/api/cms/task'
import * as TaskSensitiveResultApi from '@/api/cms/taskSensitiveResult'
import TaskForm from './TaskForm.vue'
const tableLayout = ref('auto')
const resultDialogVisible = ref<boolean>(false)
const handleReusltPage = async (id: number) => {
  taskResultQueryParams.taskId = id
  resultDialogVisible.value = true
  await getTaskResultList()
}
const taskResultQueryParams = reactive({
  taskId: 0,
  pageNo: 1,
  pageSize: 20
})
//任务结果内容
const resultLoading = ref(true) // 列表的加载中
const relateTotal = ref(0) // 列表的总页数
const resultDatas = ref([]) // 列表的数据
const resultTableRef = ref() // 表格的 Ref
/** 查询任务结果列表 */
const getTaskResultList = async () => {
  resultLoading.value = true
  try {
    const data = await TaskSensitiveResultApi.getTaskSensitiveResultPage(taskResultQueryParams)
    resultDatas.value = data.list
    relateTotal.value = data.total
  } finally {
    resultLoading.value = false
  }
}
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const tableRef = ref() // 表格的 Ref
const ids = ref<number[]>([]) // 表格的选中 ID 数组
/** 当内容主操作表格选择项发生变化时会触发该事件  */
const handleDataSelectionChange = (array: TaskVO[]) => {
  ids.value = []
  if (!array) {
    return
  }
  array.forEach((row) => ids.value.push(row.id))
}
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  taskType: 3,
  name: '',
  isGlobal: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TaskApi.getTaskPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 查看详情 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskApi.deleteTask(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
//扫描
const handleScan = async () => {
  try {
    await TaskSensitiveResultApi.createSensitiveCheckTask()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
//批量删除任务
const handleDeleteTasks = async () => {
  let taskIds = ids.value
  if (taskIds.length === 0) {
    return
  }
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskApi.deleteTasks(taskIds)
    message.success(t('common.delSuccess'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.taskType = 3
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
